package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.OverdueConfigApiService;
import com.yixun.qfbao.apidto.CheckReportProtectReqDto;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.enums.TimeUnitTypeEnum;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.service.PremisesService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class OverdueConfigApiServiceImpl implements OverdueConfigApiService {

    private static final Logger logger = LoggerFactory.getLogger(OverdueConfigApiServiceImpl.class);

    @Autowired
    private PremisesService premisesService;
    @Autowired
    private PartnerInfoService partnerInfoService;

    @Override
    public List<Map<String, String>> checkReportProtect(CheckReportProtectReqDto reqDto) {
        if(Objects.isNull(reqDto.getPremisesId())) {
            throw new TServerException("楼盘ID不能为空！");
        }
        if(reqDto.getCusPhoneList() == null || reqDto.getCusPhoneList().size() == 0) {
            throw new TServerException("客户手机号不能为空！");
        }
        // 校验是否开启报备保护时间限制
        Premises premises = premisesService.getSimpleById(reqDto.getPremisesId());
        if(premises == null) {
            throw new TServerException("未获取到楼盘信息！");
        }
        if(premises.getReportProtectType() == null || TimeUnitTypeEnum.NONE.getNo().equals(premises.getReportProtectType())) {
            return null;
        }
        // 校验是否开启到访延长时间限制和交易延长时间限制
        if ((premises.getVisitProtectType() == null || TimeUnitTypeEnum.NONE.getNo().equals(premises.getVisitProtectType()))
                && (premises.getSaveDaysType() == null || TimeUnitTypeEnum.NONE.getNo().equals(premises.getSaveDaysType()))) {
            return null;
        }
        // 获取经纪人信息
        if(StringUtils.isBlank(reqDto.getUserId())){
            throw new TServerException("获取经纪人信息失败！");
        }
        PartnerInfo agentPi = partnerInfoService.selectById(Long.valueOf(reqDto.getUserId()));
        if(agentPi == null) {
            throw new TServerException("获取经纪人信息失败！");
        }
        List<String> cusPhoneList = reqDto.getCusPhoneList();
        List<Map<String, String>> resultList = new ArrayList<>(cusPhoneList.size());
        return resultList;
    }
}
